package org.test.jf.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

public class DBUtil {

	private static DBInfo dbInfo = getDBInfo();
	
	public Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName(dbInfo.getDriver());
			conn = DriverManager.getConnection(dbInfo.getUrl(), dbInfo.getUserName(), dbInfo.getPassWord());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public static DBInfo getDBInfo() {
		DBInfo info = new DBInfo();
		Document document = XMLUtil.getXMLInfo("db.xml");
		String url = document.selectSingleNode("db-conf/url").getText();
		String userName = document.selectSingleNode("db-conf/username").getText();
		String passWord = document.selectSingleNode("db-conf/password").getText();
		String driver = document.selectSingleNode("db-conf/driver").getText();
		info.setUrl(url);
		info.setPassWord(passWord);
		info.setUserName(userName);
		info.setDriver(driver);
		return info;
	}
}
